package yto.freemarker.springbootstudy.rabbitmq.config;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 带回调的发送者
 *
 * @author dongxing
 * @create 2018-05-02 12:50
 **/
@Component
public class CallBackSender implements RabbitTemplate.ConfirmCallback{

        @Autowired
        private RabbitTemplate rabbitTemplatenew;

        public void send() {
            rabbitTemplatenew.setConfirmCallback(this);
            String msg = "callbackSender : i am callback sender";
            System.out.println(msg);
            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            System.out.println("callbackSender UUID: " + correlationData.getId());
            this.rabbitTemplatenew.convertAndSend("exchange", "topic.messages", msg, correlationData);
        }

        @Override
        public void confirm(CorrelationData correlationData, boolean b, String s) {
            System.out.println("callbakck confirm: " + correlationData.getId());

        }
}
